<?php


class ServiceController extends Zend_Controller_Action
{
	
	
	public function init()
	{
		/* Initialize action controller here */
		
	}
	
	public function indexAction() 
	{
	
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		$cod_backend = $this->auth() ; 
		
		
		// check if backend is authenticated  	
		if($cod_backend) 
		{ 
			//echo  $cod_backend ; 
			$commands  = new Application_Model_DbTable_Commands() ;
			$select = $commands->select()
 			->where("cod_backend = '".$cod_backend."' and  date_executed is null  " ) ;
			$rows = $commands->fetchAll($select);
			
			//print_r($rows) ; 
			
			foreach ($rows as $row)
			{
				echo $row->id."*".$row->command."\n" ; 
			}
						 
			
		}
		else
		{ 
			echo "2033" ; 
		}
		
	 
		
	
	} 
	
	
	public function returnAction()
	{
	
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
	
		
		$return  = new Application_Model_DbTable_Commands() ;
		
		$data = array(
		   'return' => $_REQUEST['return'] ,
		   'date_executed' => date("Y-m-d H:i:s")
		);
		
		
		$where['id = ?'] = $_REQUEST['id']  ;
		
		$return->update($data, $where);
		
		
	
	}
	
	
	public function putcommandsAction()
	{
	
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
	
	
	
	
	
	}
	
	private function auth()
	{
	
		$backends  = new Application_Model_DbTable_Backends() ;
		$select = $backends->select()
		->where("key = '".$_REQUEST['key']."' " ) ;
		$rows = $backends->fetchAll($select);
		
		if(sizeof($rows) > 0)
		{ 
			
			foreach ($rows as $row)
			{
				$row->id ;
			}
			
			return $row->id ;
			 
		}
		else
		{ 
			return false ; 
		}
	
	}
	
	
	
	
	
	
}
	
?> 	 